<?php

/*
 * 此文件用于验证短信服务API接口，供开发时参考
 * 执行验证前请确保文件为utf-8编码，并替换相应参数为您自己的信息，并取消相关调用的注释
 * 建议验证前先执行Test.php验证PHP环境
 *
 * 2017/11/30
 */

namespace Aliyun\Duanxin;

//require_once "../SignatureHelper.php";

use Aliyun\Duanxin\SignatureHelper;

// todo 接口定义，请先替换相应参数为您自己的信息
class SendSms {

    /**
     * 发送短信
     */
    public static function sendSms($mobile = '13888888888', $code = '') {

        $params = array();

        // *** 需用户填写部分 ***
        // fixme 必填：是否启用https
        $security = false;

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = "LTAIWSRAUqcSNnz4";
        $accessKeySecret = "9jwtDswVqFo5Uy9kGdS9zMZXxvYnR5";

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $mobile;

        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = "验证码123456789";

        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = "SMS_156466666";

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array(
            "code" => $code,
            "product" => 123456789
        );

        // fixme 可选: 设置发送短信流水号
        $params['OutId'] = $code;

        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
        $params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new SignatureHelper();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
                $accessKeyId, $accessKeySecret, "dysmsapi.aliyuncs.com", array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "SendSms",
            "Version" => "2017-05-25",
                )), $security
        );

        return $content;
    }

}

/*
stdClass Object
(
    [Message] => OK
    [RequestId] => FDF13431-4D81-4E5D-A298-BDCB0C1F6472
    [BizId] => 196318057997611632^0
    [Code] => OK
)
 * 
stdClass Object
(
    [Recommend] => https://error-center.aliyun.com/status/search?Keyword=InvalidAccessKeyId.NotFound&source=PopGw
    [Message] => Specified access key is not found.
    [RequestId] => C945662B-0C87-45B2-870C-A7DAF20EF6ED
    [HostId] => dysmsapi.aliyuncs.com
    [Code] => InvalidAccessKeyId.NotFound
)
 */
